﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.ViewModel;

namespace Mimoza.Apps.Console.Commands
{
    class RenameHostCommand: BaseConsoleCommand
    {
        HostsViewModel m_ViewModel = null;
        public RenameHostCommand(HostsViewModel viewModel) 
        {
            m_ViewModel = viewModel;
            m_ViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(this.view_PropertyChanged);
        }

        void view_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (null == m_ViewModel ) return;

            this.Visible = m_ViewModel.InFocus;
            if (null != m_ViewModel.Context && ((object[])m_ViewModel.Context).Length == 1)
            {
                this.Enabled = true;
            }
            else
            {
                this.Enabled = false;
            }
        }

        public override void Execute()
        {
            if (null == m_ViewModel || null == m_ViewModel.Context) return;

            RenameHostCommandForm form = new RenameHostCommandForm();
            Host host = ((object[])m_ViewModel.Context)[0] as Host;
            form.hostName.Text = host.Name;
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                host.Name = form.hostName.Text;
                host.NewName = form.hostName.Text;
                host.ModificationID = Guid.NewGuid();
                m_ViewModel.Application.ObjectLayer.HostRepository.Update(host);
            }
        }
    }
}
